package com.simplenix.rush.db;

import com.simplenix.rush.db.filters.NoteNameFilter;
import net.sourceforge.floggy.persistence.FloggyException;
import net.sourceforge.floggy.persistence.ObjectSet;
import net.sourceforge.floggy.persistence.Persistable;
import net.sourceforge.floggy.persistence.PersistableManager;

/**
 *
 * @author fronald
 */
public class Note implements Persistable {

    private String name;
    private String note;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the note
     */
    public String getNote() {
        return note;
    }

    /**
     * @param note the note to set
     */
    public void setNote(String note) {
        this.note = note;
    }

    public static boolean exists(String name) {
        Note nt = getNoteByName(name);
        return nt != null;
    }

    public static void deleteNoteByName(String name) {
        try {
            PersistableManager.getInstance().delete(getNoteByName(name));
        } catch (FloggyException ex) {
            ex.printStackTrace();
        }
    }

    public static Note getNoteByName(String name) {
        try {
            ObjectSet notes = PersistableManager.getInstance().find(Note.class, new NoteNameFilter(name), null);
            if (notes != null) {
                if (notes.size() >= 1) {
                    return (Note) notes.get(0);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean incluiu(Note note) {
        try {
            if (exists(note.getName())) {
                deleteNoteByName(note.getName());
            }
            PersistableManager.getInstance().save(note);
            return true;
        } catch (FloggyException ex) {
            ex.printStackTrace();
        }
        return false;
    }
}
